//
//  NSDate+timestamp.m
//  helper
//
//  Created by huangyi on 12-10-29.
//  Copyright (c) 2012年 huangyi. All rights reserved.
//

#import "NSDate+timestamp.h"

@implementation NSDate (timestamp)

+(NSTimeInterval)timestamp{
    return [[NSDate date] timeIntervalSince1970];
}

-(NSDate*)dateOfTomorrow{
    return [self dateByAddingTimeInterval:86400.0];
}
-(NSDate*)dateOfYesterday{
    return [self dateByAddingTimeInterval:-86400.0];
}
-(BOOL)isToday{
    NSDate *today=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    BOOL isToday=[[formatter stringFromDate:today] isEqualToString:[formatter stringFromDate:self]];
    return isToday;
}
-(BOOL)isYesterday{
    NSDate *today=[NSDate date];
    NSDate *yesterday=[today dateOfYesterday];
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    BOOL isYesterday=[[formatter stringFromDate:yesterday] isEqualToString:[formatter stringFromDate:self]];
    return isYesterday;
}
-(BOOL)isTomorrow{
    NSDate *today=[NSDate date];
    NSDate *tomorrow=[today dateOfTomorrow];
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    BOOL isTomorrow=[[formatter stringFromDate:tomorrow] isEqualToString:[formatter stringFromDate:self]];
    return isTomorrow;
}

+(NSDate *)dateFromRFC822String:(NSString *)dateString {
    if (!dateString) {
        return nil;
    }
	static NSDateFormatter *dateFormatter = nil;
	if (!dateFormatter) {
		NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
		dateFormatter = [[NSDateFormatter alloc] init];
		[dateFormatter setLocale:en_US_POSIX];
		[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
	}
	NSDate *date = nil;
	NSString *RFC822String = [[NSString stringWithString:dateString] uppercaseString];
	if ([RFC822String rangeOfString:@","].location != NSNotFound) {
		if (!date) { // Sun, 19 May 2002 15:21:36 GMT
			[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
			date = [dateFormatter dateFromString:RFC822String];
		}
		if (!date) { // Sun, 19 May 2002 15:21 GMT
			[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm zzz"];
			date = [dateFormatter dateFromString:RFC822String];
		}
		if (!date) { // Sun, 19 May 2002 15:21:36
			[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss"];
			date = [dateFormatter dateFromString:RFC822String];
		}
		if (!date) { // Sun, 19 May 2002 15:21
			[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm"];
			date = [dateFormatter dateFromString:RFC822String];
		}
	} else {
		if (!date) { // 19 May 2002 15:21:36 GMT
			[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss zzz"];
			date = [dateFormatter dateFromString:RFC822String];
		}
		if (!date) { // 19 May 2002 15:21 GMT
			[dateFormatter setDateFormat:@"d MMM yyyy HH:mm zzz"];
			date = [dateFormatter dateFromString:RFC822String];
		}
		if (!date) { // 19 May 2002 15:21:36
			[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"];
			date = [dateFormatter dateFromString:RFC822String];
		}
		if (!date) { // 19 May 2002 15:21
			[dateFormatter setDateFormat:@"d MMM yyyy HH:mm"];
			date = [dateFormatter dateFromString:RFC822String];
		}
	}
	if (!date){
        NSLog(@"Could not parse RFC822 date (%@) Possibly invalid format.", dateString);
    }
	return date;
}

+(NSDate*)dateFromRFC3339String:(NSString*)dateString {
    if (!dateString) {
        return nil;
    }
	static NSDateFormatter *dateFormatter = nil;
	if (!dateFormatter) {
		NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
		dateFormatter = [[NSDateFormatter alloc] init];
		[dateFormatter setLocale:en_US_POSIX];
		[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
	}
	NSDate *date = nil;
	NSString *RFC3339String = [[NSString stringWithString:dateString] uppercaseString];
	RFC3339String = [RFC3339String stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"];
	// Remove colon in timezone as iOS 4+ NSDateFormatter breaks. See https://devforums.apple.com/thread/45837
	if (RFC3339String.length > 20) {
		RFC3339String = [RFC3339String stringByReplacingOccurrencesOfString:@":"
																 withString:@""
																	options:0
																	  range:NSMakeRange(20, RFC3339String.length-20)];
	}
	if (!date) { // 1996-12-19T16:39:57-0800
		[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"];
		date = [dateFormatter dateFromString:RFC3339String];
	}
	if (!date) { // 1937-01-01T12:00:27.87+0020
		[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ"];
		date = [dateFormatter dateFromString:RFC3339String];
	}
	if (!date) { // 1937-01-01T12:00:27
		[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
		date = [dateFormatter dateFromString:RFC3339String];
	}
	if (!date) {
        NSLog(@"Could not parse RFC3339 date (%@) Possibly invalid format.", dateString);
    }
	return date;
}


-(NSString*)stringWithFormat:(NSString*)format{
    NSLocale *locale = [NSLocale currentLocale];
    NSTimeZone *timezone=[NSTimeZone localTimeZone];
    return [self stringWithFormat:format withLocale:locale withTimeZone:timezone];
}
-(NSString*)stringWithFormat:(NSString*)format withLocale:(NSLocale*)locale{
    NSTimeZone *timezone=[NSTimeZone localTimeZone];
    return [self stringWithFormat:format withLocale:locale withTimeZone:timezone];
}
-(NSString*)stringWithFormat:(NSString*)format withTimeZone:(NSTimeZone*)timezone{
    NSLocale *locale = [NSLocale currentLocale];
    return [self stringWithFormat:format withLocale:locale withTimeZone:timezone];
}
-(NSString*)stringWithFormat:(NSString*)format withLocale:(NSLocale*)locale withTimeZone:(NSTimeZone*)timezone{
	static NSDateFormatter *dateFormatter = nil;
	if (!dateFormatter) {
		dateFormatter = [[NSDateFormatter alloc] init];
	}
    [dateFormatter setLocale:locale];
    [dateFormatter setTimeZone:timezone];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:self];
}

-(NSString*)localizedStringWithFormat:(NSString*)format{
    NSLocale *locale=[NSLocale currentLocale];
    format=[NSDateFormatter dateFormatFromTemplate:format options:0 locale:locale];
    return [self stringWithFormat:format];
}
+(NSInteger)offsetOfTimeZone:(NSTimeZone*)timezone{
    if (timezone==nil) {
        timezone=[NSTimeZone localTimeZone];
    }
    return [timezone secondsFromGMT];
}
-(NSDate*)startOfDate{
    NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier:NSISO8601Calendar];
    NSDateComponents *components=[calendar components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self];
    return [calendar dateFromComponents:components];
}

@end

int unixtime(){
    NSTimeInterval ts=[NSDate timestamp];
    return ts;
}

NSString* NOW(){
    NSDate *now=[NSDate date];
    return [now stringWithFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
}

NSDate* NSDateFromTimestamp(NSTimeInterval timestamp){
    return [NSDate dateWithTimeIntervalSince1970:timestamp];
}
